﻿using System;
using System.Collections.Generic;
using System.Data;
using l1cs.server.templates;
using MySql.Data.MySqlClient;
using l1cs.server.utils;

namespace l1cs.server.datatables
{
    class SpawnTimeTable
    {
        private static Logger _log = Logger.getLogger( typeof( SpawnTable ).FullName );

        private static SpawnTimeTable _instance;

        private Dictionary<int, L1SpawnTime> _times = new Dictionary<int, L1SpawnTime>();

        public static SpawnTimeTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new SpawnTimeTable();
            }
            return _instance;
        }

        private SpawnTimeTable()
        {/*TTOK*/
            String sqlstr = "SELECT * FROM spawnlist_time";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                foreach ( DataRow item in wrapper.GetDataTable(sqlstr).Rows )
                {
                    int id = SqlUtils.toInt( item["spawn_id"] );
                    L1SpawnTime.L1SpawnTimeBuilder builder = new L1SpawnTime.L1SpawnTimeBuilder( id );
                    builder._timeStart = (TimeSpan)item["time_start"];
                    builder._timeEnd = (TimeSpan)item["time_end"];
                    builder._isDeleteAtEndTime = Convert.ToBoolean( item["delete_at_endtime"] );

                    _times[id] = builder.build();
                }
            }
        }

        public L1SpawnTime get( int id )
        {/*TTOK*/
            if ( _times.ContainsKey( id ) )
            {
                return _times[id];
            }
            else
            {
                return null;
            }
        }
    }
}
